package com.leyou.auth.web;

import com.leyou.auth.dto.UserDetail;
import com.leyou.auth.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserAuthController {

    @Autowired
    private UserAuthService userAuthService;


    /**
     * 用户的登录信息，存入cookie中
     * cookie中的内容是谁存的？怎么存储的？
     * response，setCookie,把cookie存储
     */

    @PostMapping("/login")
    public ResponseEntity<Void> authLogin(
            @RequestParam("username")String username,
            @RequestParam("password")String password,
                    HttpServletResponse response){

        this.userAuthService.authLogin(username,password,response);
        return ResponseEntity.ok().build();
    }


    /**
     * 从请求中获取cookie，cookie的名称为LY_TOKEN，获取的内容就是token，token解析
     * @param request
     * @return
     */
    @GetMapping("/verify")
    public ResponseEntity<String> verify(HttpServletRequest request){

        return ResponseEntity.ok(this.userAuthService.verify(request));

    }

    @PostMapping("/logout")
    public ResponseEntity<Void> logout(HttpServletRequest request,HttpServletResponse response){

        this.userAuthService.logout(request,response);
        return ResponseEntity.ok().build();
    }
}
